package com.xbongbong.pro.message.pushset;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.message.domain.entity.PushSetEntity;
import com.xbongbong.pro.constant.CommonConstant;
import com.xbongbong.pro.message.base.AbstractPushSet;
import com.xbongbong.pro.message.constant.PushSetConstant;
import com.xbongbong.pro.message.enums.PushObjectTypeEnum;
import com.xbongbong.pro.message.enums.PushTimeTypeEnum;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.model.PushSetModel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author long.rao
 * @version v1.0
 * @date 2020/7/20 16:03
 * @since v1.0
 */
public class WorkReportOverduePushSet extends AbstractPushSet {
    private static  Integer oneDay = 86400;


    private static final Integer ONE_DAY_TYPE = 1;
    private static final Integer TWO_DAY_TYPE = 2;
    private static final Integer THREE_DAY_TYPE = 3;
    private static final Integer FOUR_DAY_TYPE = 4;
    private static final Integer FIVE_DAY_TYPE = 5;

    @Override
    public PushSetEntity toPushSetEntity(String pushObject, String pushTime, String attrEntity) {
        return null;
    }

    @Override
    public PushSetEntity getPushSet(String corpid, Integer subType, PushSetModel pushSetModel) {
        Integer type = PushTypeEnum.WORK_REPORT_OVERDUE_PUSH.getCode();

        Map<String,Object> param = new HashMap<>(16);
        param.put("corpid", corpid);
        param.put("type", type);
        param.put("subType", subType);
        List<PushSetEntity> pushSetList = pushSetModel.findEntitys(param);

        PushSetEntity pushSetEntity;
        if(pushSetList.isEmpty()){
            pushSetEntity = new PushSetEntity();
            pushSetEntity.setType(type);
            pushSetEntity.setSubType(subType);
            pushSetEntity.setEnable(0);
            JSONObject pushObject = new JSONObject();
            if (Objects.equals(PushTypeEnum.WORK_REPORT_OVERDUE_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.WORK_REPORT_OVERDUE_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.WORKREPORT_OVERDUE));
                //type
                JSONArray typeArray = new JSONArray();
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("name", PushObjectTypeEnum.SUBMITTER.getName());
                jsonObject.put("enable", 0);
                jsonObject.put("alias", PushObjectTypeEnum.SUBMITTER.getAlias());
                typeArray.add(jsonObject);
                pushObject.put("type", typeArray);
                //dep
                JSONObject depObject = new JSONObject();
                depObject.put("enable", 0);
                depObject.put("value", new ArrayList<>());
                pushObject.put("dep", depObject);
                //role
                JSONObject roleObject = new JSONObject();
                roleObject.put("enable", 0);
                roleObject.put("value", new ArrayList<>());
                pushObject.put("role", roleObject);
                //user
                JSONObject userObject = new JSONObject();
                userObject.put("enable", 0);
                userObject.put("value", new ArrayList<>());
                pushObject.put("user", userObject);
                //manager
                JSONObject managerObject = new JSONObject();
                managerObject.put("enable", 0);
                managerObject.put("value", new ArrayList<>());
                managerObject.put("name", I18nMessageUtil.getMessage(CommonConstant.SUBMITTER_MANAGER));
                managerObject.put("justCheckSelf",true);
                pushObject.put("manager", managerObject);
                //pushtime
                JSONObject pushTimeObject = new JSONObject();
                pushTimeObject.put("loop", 0);
                pushTimeObject.put("timeType", PushTimeTypeEnum.DELAYED.getCode());
                pushTimeObject.put("period", oneDay);
                pushTimeObject.put("time", 0);
                pushTimeObject.put("type", 1);
                pushTimeObject.put("pushTime", "9:00");

                JSONArray timeArray = new JSONArray();
                JSONObject timeObject1 = new JSONObject();
                timeObject1.put("id", PushTimeTypeEnum.DELAYED.getCode());
                timeObject1.put("name", I18nMessageUtil.getMessage(CommonConstant.DELAYED));
                timeArray.add(timeObject1);
                pushTimeObject.put("timeArray", timeArray);
                pushTimeObject.put("timeOptions", Arrays.asList(ONE_DAY_TYPE, TWO_DAY_TYPE, THREE_DAY_TYPE, FOUR_DAY_TYPE, FIVE_DAY_TYPE));
                // 自选推送时间选项
                //8点前是钉钉消息推送的防骚扰时间，需要避开，故此处仅可选择09:00~24:00的整点；
                pushTimeObject.put("hourOptions", Arrays.asList("9:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00","19:00","20:00","21:00","22:00","23:00","24:00"));
                pushTimeObject.put("promptInformation", I18nMessageUtil.getMessage(PushSetConstant.WORKREPORT_OVERDUE_INFORMATION));
                pushSetEntity.setPushTime(pushTimeObject.toJSONString());

            }
            pushSetEntity.setPushObject(pushObject.toJSONString());
        } else {
            pushSetEntity = pushSetList.get(0);
            if (Objects.equals(PushTypeEnum.WORK_REPORT_OVERDUE_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.WORK_REPORT_OVERDUE_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.WORKREPORT_OVERDUE));
            }
        }
        return pushSetEntity;
    }

    @Override
    public PushSetEntity getPushSet(String corpid, String attr, PushSetModel pushSetModel) {
        return null;
    }

    @Override
    public List<PushSetEntity> getPushSet(String corpid, List<String> attrList, PushSetModel pushSetModel, Long templateId) {
        return null;
    }

    @Override
    public JSONObject getTimeJson(JSONObject jsonObject) {
        return null;
    }
}
